package cn.wolfcode.handler;

import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.redis.SeckillRedisKey;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

/**
 * @author LuoRis
 * @date 2021/03/09 13:04
 * @description canal同步
 */
@Slf4j
@Component
@CanalTable(value = "t_order_info")
public class OrderInfoHandler implements EntryHandler<OrderInfo> {

    @Autowired
    private StringRedisTemplate redisTemplate;


    /**
     * 在订单创建的时候把数据存入reids
     * @param orderInfo
     */
    @Override
    public void insert(OrderInfo orderInfo) {
        log.info("-----创建订单同步操作------");

        redisTemplate.opsForHash().put(SeckillRedisKey.SECKILL_ORDER_HASH.getRealKey(""),
                orderInfo.getOrderNo()+"",
                JSON.toJSONString(orderInfo));

        redisTemplate.opsForSet().add(SeckillRedisKey.SECKILL_ORDER_SET.getRealKey(orderInfo.getSeckillTime()+""),
                orderInfo.getUserId() + ":" + orderInfo.getSeckillId());

    }

    /**
     * 修改的时候触发
     * @param before
     * @param after
     */
    @Override
    public void update(OrderInfo before, OrderInfo after) {
        log.info("-----更新订单同步操作------");

        redisTemplate.opsForHash().put(SeckillRedisKey.SECKILL_ORDER_HASH.getRealKey(""),
                after.getOrderNo()+"",
                JSON.toJSONString(after));

    }

    /**
     * 删除的时候触发
     * @param orderInfo
     */
    @Override
    public void delete(OrderInfo orderInfo) {
        log.info("-----删除订单同步操作------");
        redisTemplate.opsForHash().put(SeckillRedisKey.SECKILL_ORDER_DLETE_HASH.getRealKey(""),
                orderInfo.getOrderNo()+"",
                JSON.toJSONString(orderInfo));

    }
}
